home *** CD-ROM | disk | FTP | other *** search
- Path: news.imag.fr!usenet
- From: Agnes Poyet <Agnes.Poyet@imag.fr>
- Newsgroups: comp.lang.c++
- Subject: Q : pointer to member function
- Date: 9 Apr 1996 09:12:17 GMT
- Organization: IMAG, Grenoble, France
- Message-ID: <4kd9lh$q3q@imag.imag.fr>
- NNTP-Posting-Host: curie.imag.fr
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (X11; I; OSF1 V3.2 alpha)
- X-URL: news:comp.lang.c++
-
- I would like to have a class Minimization whose one member is a
- pointer to a function (that is the function to be minimized) :
-
- class Minim
- {
- public :
- //...
- double (* f_to_be_minimized)(double param);
- double minimize(double& param)
- // minimize the *f_to_be_minimized member function
- {
- //...
- (*f_to_be_minimized)(current_param); // (2)
- //...
- }
- //...
- }
-
- I have an other class which implements a problem to solve and
- contains a cost function which has to be minimized :
-
- class Problem
- {
- public :
- //...
- double f_cost(double param);
- //...
- }
-
- Then I would like to solve my problem in the following way :
-
- main()
- {
- Minim m;
- Problem my_pb;
- double final_param = INIT_PARAM;
-
- //...
- m.f_to_be_minimized = &(my_pb.f_cost); // (1)
- m.minimize(final_param);
- //...
- }
-
- Unfortunately, the compiler complains :
- assignment to `double (*)(double)' from `double (Problem::*)(double)'
- for the line (1), which is quite understandable.
-
-
- So I intend to define the Minim class in the less general following way :
-
- class Minim
- {
- public :
- //...
- double (Problem::* f_to_be_minimized)(double param);
- double minimize(double& param);
- // minimize the *f_to_be_minimized member function
- //...
- }
-
- The compiler complains again :
- invalid use of `unary *' on pointer to member function
- for the line (2).
-
-
- Can someone explain me this error message and give me an idea to
- solve my problem ? Thank you in advance.
-
-
- Agnes
-
-